<!DOCTYPE html><html lang="en" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Spring | Cying</title><meta name="keywords" content="JavaEE"><meta name="author" content="Cying-承影"><meta name="copyright" content="Cying-承影"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="Spring  一、Spring 概述1.1 - 什么是 Spring 是一个开源框架 Spring是分层的Java SE&#x2F;EE应用Full-Stack轻量级开源框架，以IOC - 反转控制 (Inverse Of Control)和AOP - 面向切面编程 (Aspecr Oriented Programming)为内核，提供了展现层Spring MVC和持久层Spring JDBC以及业务层事">
<meta property="og:type" content="article">
<meta property="og:title" content="Spring">
<meta property="og:url" content="http://cyingk.github.io/2020/01/27/Spring/index.html">
<meta property="og:site_name" content="Cying">
<meta property="og:description" content="Spring  一、Spring 概述1.1 - 什么是 Spring 是一个开源框架 Spring是分层的Java SE&#x2F;EE应用Full-Stack轻量级开源框架，以IOC - 反转控制 (Inverse Of Control)和AOP - 面向切面编程 (Aspecr Oriented Programming)为内核，提供了展现层Spring MVC和持久层Spring JDBC以及业务层事">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2516020377,3847466993&fm=11&gp=0.jpg">
<meta property="article:published_time" content="2020-01-26T17:24:53.000Z">
<meta property="article:modified_time" content="2020-07-03T16:21:13.000Z">
<meta property="article:author" content="Cying-承影">
<meta property="article:tag" content="JavaEE">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2516020377,3847466993&fm=11&gp=0.jpg"><link rel="shortcut icon" href="/img/favicon.ico"><link rel="canonical" href="http://cyingk.github.io/2020/01/27/Spring/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//www.clarity.ms"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><script>(function(c,l,a,r,i,t,y){
    c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)};
    t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i;
    y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y);
})(window, document, "clarity", "script", "4zndahq92q");</script><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true},
  copy: {
    success: 'Copy successfully',
    error: 'Copy error',
    noSupport: 'The browser does not support'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: 'days',
  date_suffix: {
    just: 'Just',
    min: 'minutes ago',
    hour: 'hours ago',
    day: 'days ago',
    month: 'months ago'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"Traditional Chinese Activated Manually","cht_to_chs":"Simplified Chinese Activated Manually","day_to_night":"Dark Mode Activated Manually","night_to_day":"Light Mode Activated Manually","bgLight":"#49b1f5","bgDark":"#121212","position":"bottom-left"},
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: true,
  islazyload: false,
  isanchor: false
};

const saveToLocal = {
  // ttl 單位是 天
  set: function setWithExpiry(key, value, ttl) {
    if (ttl === 0) return
    const now = new Date()
    const expiryDay = ttl * 86400000
    const item = {
      value: value,
      expiry: now.getTime() + expiryDay,
    }
    localStorage.setItem(key, JSON.stringify(item))
  },

  get: function getWithExpiry(key) {
    const itemStr = localStorage.getItem(key)

    if (!itemStr) {
      return undefined
    }
    const item = JSON.parse(itemStr)
    const now = new Date()

    if (now.getTime() > item.expiry) {
      localStorage.removeItem(key)
      return undefined
    }
    return item.value
  }
}

// https://stackoverflow.com/questions/16839698/jquery-getscript-alternative-in-native-javascript
const getScript = url => new Promise((resolve, reject) => {
  const script = document.createElement('script')
  script.src = url
  script.async = true
  script.onerror = reject
  script.onload = script.onreadystatechange = function() {
    const loadState = this.readyState
    if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
    script.onload = script.onreadystatechange = null
    resolve()
  }
  document.head.appendChild(script)
})</script><script id="config_change">var GLOBAL_CONFIG_SITE = { 
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2020-07-04 00:21:13'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(function () {  window.activateDarkMode = function () {
    document.documentElement.setAttribute('data-theme', 'dark')
    if (document.querySelector('meta[name="theme-color"]') !== null) {
      document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
    }
  }
  window.activateLightMode = function () {
    document.documentElement.setAttribute('data-theme', 'light')
   if (document.querySelector('meta[name="theme-color"]') !== null) {
      document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
    }
  }
  const autoChangeMode = '1'
  const t = saveToLocal.get('theme')
  if (autoChangeMode === '1') {
    const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
    const isLightMode = window.matchMedia('(prefers-color-scheme: light)').matches
    const isNotSpecified = window.matchMedia('(prefers-color-scheme: no-preference)').matches
    const hasNoSupport = !isDarkMode && !isLightMode && !isNotSpecified
    if (t === undefined) {
      if (isLightMode) activateLightMode()
      else if (isDarkMode) activateDarkMode()
      else if (isNotSpecified || hasNoSupport) {
        const now = new Date()
        const hour = now.getHours()
        const isNight = hour <= 6 || hour >= 18
        isNight ? activateDarkMode() : activateLightMode()
      }
      window.matchMedia('(prefers-color-scheme: dark)').addListener(function (e) {
        if (saveToLocal.get('theme') === undefined) {
          e.matches ? activateDarkMode() : activateLightMode()
        }
      })
    } else if (t === 'light') activateLightMode()
    else activateDarkMode()
  } else if (autoChangeMode === '2') {
    const now = new Date()
    const hour = now.getHours()
    const isNight = hour <= 6 || hour >= 18
    if (t === undefined) isNight ? activateDarkMode() : activateLightMode()
    else if (t === 'light') activateLightMode()
    else activateDarkMode()
  } else {
    if (t === 'dark') activateDarkMode()
    else if (t === 'light') activateLightMode()
  }const asideStatus = saveToLocal.get('aside-status')
if (asideStatus !== undefined) {
   if (asideStatus === 'hide') {
     document.documentElement.classList.add('hide-aside')
   } else {
     document.documentElement.classList.remove('hide-aside')
   }
}const fontSizeVal = saveToLocal.get('global-font-size')
if (fontSizeVal !== undefined) {
  document.documentElement.style.setProperty('--global-font-size', fontSizeVal + 'px')
}})()</script><meta name="generator" content="Hexo 5.3.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">Loading...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src="/img/avatar.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">Articles</div><div class="length-num">23</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">Tags</div><div class="length-num">12</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">Categories</div><div class="length-num">3</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fa fa-home"></i><span> Home</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> Archives</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> Tags</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> Categories</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://5b0988e595225.cdn.sohucs.com/images/20190831/84729f5c5bb346208416445feb485df6.jpeg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">Cying</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fa fa-home"></i><span> Home</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> Archives</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> Tags</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> Categories</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Spring</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">Created</span><time class="post-meta-date-created" datetime="2020-01-26T17:24:53.000Z" title="Created 2020-01-27 01:24:53">2020-01-27</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">Updated</span><time class="post-meta-date-updated" datetime="2020-07-03T16:21:13.000Z" title="Updated 2020-07-04 00:21:13">2020-07-04</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Note/">Note</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">Word count:</span><span class="word-count">2.8k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">Reading time:</span><span>10min</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">Post View:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><html><head></head><body><h1><center>Spring</center></h1>

<h1 id="一、Spring-概述"><a href="#一、Spring-概述" class="headerlink" title="一、Spring 概述"></a>一、Spring 概述</h1><h2 id="1-1-什么是-Spring"><a href="#1-1-什么是-Spring" class="headerlink" title="1.1 - 什么是 Spring"></a>1.1 - 什么是 Spring</h2><blockquote>
<p>是一个开源框架</p>
<p><code>Spring</code>是分层的<code>Java SE/EE</code>应用<code>Full-Stack</code>轻量级开源框架，以<code>IOC - 反转控制 (Inverse Of Control)</code>和<code>AOP - 面向切面编程 (Aspecr Oriented Programming)</code>为<strong>内核</strong>，提供了展现层<code>Spring MVC</code>和持久层<code>Spring JDBC</code>以及业务层事物管理等众多的企业级应用技术，还能整合开源世界众多著名的第三方框架和类库，逐渐成为使用最多的<code>Java EE</code>企业应用开源框架。</p>
</blockquote>
<p>1.2 - Sping 的发展历程</p>
<blockquote>
<ul>
<li>1997，<code>IBM</code>提出<code>EJB</code>的思想</li>
<li>1998，<code>SUN</code>制定开发标准<code>EJB1.0</code></li>
<li>1999，<code>EJB 1.1</code>发布</li>
<li>2001，<code>EJB 2.0</code>发布</li>
<li>2003，<code>EJB 2.1</code>发布</li>
<li>2006，<code>EJB 3.0</code>发布</li>
</ul>
</blockquote>
<blockquote>
<p><strong>Rod Johson ( Spring 之父 )</strong></p>
<p><code>Expert One-to-One J2EE Design and Development (2002)</code></p>
<p>阐述了 <code>J2EE</code> 使用 <code>EJB</code> 开发设计的优点及解决方案</p>
<p><code>Expert One-to-One J2EE Development without EJB (2004)</code></p>
<p>阐述了 <code>J2EE</code> 开发不适用 <code>EJB</code> 的解决方案 ( Spring 雏形 )</p>
</blockquote>
<h2 id="1-3-Spring-的优势"><a href="#1-3-Spring-的优势" class="headerlink" title="1.3 - Spring 的优势"></a>1.3 - Spring 的优势</h2><blockquote>
<p><strong>方便解耦，简化开发</strong></p>
<p>通过 <code>Spring</code> 提供的 <code>Ioc</code> 容器，可以将对象间得到依赖关系交由 <code>Spring</code> 进行控制，避免硬编码所在成的过度程序耦合。用户也不必再为单例模式类、属性文件解析等这些很底层的需求编写代码，可以更专注于上层的应用</p>
</blockquote>
<blockquote>
<p><strong>AOP 编程的支持</strong></p>
<p>通过 <code>Spring</code> 的 <code>AOP</code> 工能，方便进行面向切面的编程，许多不容易用传统 <code>OOP</code> 实现的功能可以通过 <code>AOP</code> 轻松应付</p>
</blockquote>
<blockquote>
<p><strong>声明式事务的支持</strong></p>
<p>可以将我们从单调烦闷的事物管理代码中解脱出来，通过声明式方式灵活的进行事物的管理，提高开发效率和质量</p>
</blockquote>
<blockquote>
<p><strong>方便程序的测试</strong></p>
<p>可以用非容器依赖的编程方式进行几乎所有的测试工作，测试不再是昂贵的操作，而是随手可做的事情</p>
</blockquote>
<blockquote>
<p><strong>方便集成各种优秀框架</strong></p>
<p><code>Spring</code> 可以降低各种框架的使用难度，提供了对各种优秀框架（<code>Struts、Hibermate、Hessian、Quartz</code>等）的直接支持</p>
</blockquote>
<blockquote>
<p><strong>降低 JavaEE API 的使用难度</strong></p>
<p><code>Spring</code> 对 <code>JavaEE API</code> （ 如 <code>JDBC</code>、<code>JavaMail</code>、远程调用等 ） 进行了薄薄的封装层，使这些 <code>API</code> 的使用难度大为降低</p>
</blockquote>
<blockquote>
<p><strong>Java 源码是经典学习范例</strong></p>
<p><code>Spring</code> 的源码设计精妙，结构清晰，匠心独用，处处体现着对 <code>Java</code> 设计模式的灵活运用以及对 <code>Java</code> 技术的高深造诣。它的源代码无疑是 <code>Java</code> 技术的最佳实践范例</p>
</blockquote>
<h2 id="1-4-Spring-体系结构"><a href="#1-4-Spring-体系结构" class="headerlink" title="1.4 - Spring 体系结构"></a>1.4 - Spring 体系结构</h2><p><a href="C:%5CUsers%5CNLM13%5COneDrive%5CTypora%5CImages%5CSpring%5CSpring_Framework.png" data-fancybox="group" data-caption="Spring_Framework" class="fancybox"><img alt="Spring_Framework" title="Spring_Framework" data-src="C:%5CUsers%5CNLM13%5COneDrive%5CTypora%5CImages%5CSpring%5CSpring_Framework.png" class="lazyload"></a></p>
<p><strong>Core</strong> <strong>Container</strong></p>
<blockquote>
<p>是 <code>Spring</code> 的核心容器，<code>IoC</code> 部分</p>
</blockquote>
<h2 id="1-5-耦合"><a href="#1-5-耦合" class="headerlink" title="1.5 - 耦合"></a>1.5 - 耦合</h2><blockquote>
<p>程序间的依赖关系</p>
<ul>
<li>类之间的依赖</li>
<li>方法之间的依赖</li>
</ul>
</blockquote>
<blockquote>
<p><strong>解耦</strong></p>
<p>降低程序间的依赖关系</p>
</blockquote>
<blockquote>
<p><strong>应该做到</strong></p>
<p>编译时不依赖</p>
<p>运行时才依赖</p>
</blockquote>
<blockquote>
<p><strong>解耦的思路</strong></p>
<ul>
<li>使用反射来创建对象，避免使用关键字<code>new</code></li>
<li>通过读取配置文件，获取要创建的对象全限定类名</li>
</ul>
</blockquote>
<h2 id="1-6-Bean对象"><a href="#1-6-Bean对象" class="headerlink" title="1.6 - Bean对象"></a>1.6 - Bean对象</h2><blockquote>
<p>Bean 在计算机英语中有可重用组件的含义</p>
<ul>
<li><code>JavaBean</code> > 实体类 – 用 <code>Java</code> 类编写的可重用组件，就是创建我们的 <code>Service</code> 和 <code>Dao</code> <ul>
<li>需要一个配置文件来配置我们的 <code>Service</code> 和 <code>dao</code><ul>
<li>配置的内容 – 唯一标识=全限定类名（ key = value ）</li>
</ul>
</li>
<li>通过读取配置文件中的信息，反射创建对象</li>
</ul>
</li>
</ul>
</blockquote>
<h2 id="1-7-解决技术"><a href="#1-7-解决技术" class="headerlink" title="1.7 - 解决技术"></a>1.7 - 解决技术</h2><p><strong>Spring 在 JavaEE 的三层结构中，每一层都提供不同的解决技术</strong></p>
<blockquote>
<ul>
<li>web 层 – SpringMVC</li>
<li>service 层 – Spring 的 IOC</li>
<li>dao 层 – Spring 的 JDBCTemplate</li>
</ul>
</blockquote>
<h1 id="二、IOC-控制反转"><a href="#二、IOC-控制反转" class="headerlink" title="二、IOC 控制反转"></a>二、IOC 控制反转</h1><blockquote>
<ul>
<li>比如有一个类，在类里面有方法（不是静态方法），调用类里面的方法，创建类的对象，使用对象调用方法，创建类对象的过程，需要实例化对象</li>
<li>把对象的创建不是通过实例化实现，而是交给<code>Spring</code>配置创建类对象</li>
</ul>
</blockquote>
<h2 id="2-1-IOC-的底层原理"><a href="#2-1-IOC-的底层原理" class="headerlink" title="2.1 - IOC 的底层原理"></a>2.1 - IOC 的底层原理</h2><blockquote>
<ul>
<li>xml 配置文件</li>
<li>dom4j 解决 xml</li>
<li>工厂设计模式</li>
<li>反射</li>
</ul>
</blockquote>
<h3 id="2-1-1-IOC-的实现"><a href="#2-1-1-IOC-的实现" class="headerlink" title="2.1.1 - IOC 的实现"></a>2.1.1 - IOC 的实现</h3><blockquote>
<ul>
<li>将类名写在 xml 文件中</li>
</ul>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">bean</span> <span class="attr">id</span>=<span class="string">'UserService'</span> <span class="attr">class</span>=<span class="string">'包名+文件名'</span>/></span></span><br></pre></td></tr></tbody></table></figure></div>

<ul>
<li>在工厂中，通过 <code>dom4j</code> 解析 <code>xml</code> 文件，通过 <code>id</code> 获取到类名<ul>
<li>String className = ‘class属性值’</li>
</ul>
</li>
<li>利用反射创建对象</li>
</ul>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Class clazz = Class.forName(ClassName);</span><br></pre></td></tr></tbody></table></figure></div>

<ul>
<li>创建并返回类对象</li>
</ul>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">return</span> clazz.newInstance();</span><br></pre></td></tr></tbody></table></figure></div>
</blockquote>
<h3 id="2-1-2-IOC-入门案例"><a href="#2-1-2-IOC-入门案例" class="headerlink" title="2.1.2 - IOC 入门案例"></a>2.1.2 - IOC 入门案例</h3><blockquote>
<ul>
<li>导入 <code>jar</code> 包</li>
<li>创建类，在类里面创建方法</li>
<li>创建 <code>Spring</code> 配置文件，配置创建类</li>
<li>写代码测试对象创建</li>
</ul>
</blockquote>
<h2 id="2-2-bean-管理"><a href="#2-2-bean-管理" class="headerlink" title="2.2 - bean 管理"></a>2.2 - bean 管理</h2><h3 id="2-2-1-实例化的三种方式"><a href="#2-2-1-实例化的三种方式" class="headerlink" title="2.2.1 - 实例化的三种方式"></a>2.2.1 - 实例化的三种方式</h3><blockquote>
<ul>
<li>使用类的无参构造</li>
<li>使用静态工厂创建</li>
<li>使用实例工厂创建</li>
</ul>
</blockquote>
<h3 id="2-2-2-Bean-的常用属性"><a href="#2-2-2-Bean-的常用属性" class="headerlink" title="2.2.2 - Bean 的常用属性"></a>2.2.2 - Bean 的常用属性</h3><blockquote>
<ul>
<li><code>id</code> – 起名，不得含特殊符号</li>
<li><code>class</code> – 类的全路径</li>
<li><code>name</code> – 起名，可有特殊符号</li>
<li><code>scope</code> – 实例的类型<ul>
<li><code>singleton</code> – 单例</li>
<li><code>prototype</code> – 多例</li>
<li><code>requese</code> – 创建对象把对象放到 <code>request</code> 域里面</li>
<li><code>session</code> – 创建对象把对象放到 <code>session</code> 域里面</li>
<li><code>globalSession</code> – 创建对象把对象放到 <code>globalSession</code></li>
</ul>
</li>
</ul>
</blockquote>
<h2 id="2-3-属性注入"><a href="#2-3-属性注入" class="headerlink" title="2.3 - 属性注入"></a>2.3 - 属性注入</h2><h3 id="2-3-1-constructor-注入"><a href="#2-3-1-constructor-注入" class="headerlink" title="2.3.1 - constructor 注入"></a>2.3.1 - constructor 注入</h3><blockquote>
<p><strong>设有下类</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 包路径;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">A</span> </span>{</span><br><span class="line">String key;</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">A</span><span class="params">(String key)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.key = key</span><br><span class="line">}</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<p><strong>则在配置文件中应如下所示</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"自定义id"</span> <span class="attr">class</span>=<span class="string">"包路径.A"</span>></span></span><br><span class="line"><span class="tag"><<span class="name">constructor-arg</span> <span class="attr">name</span>=<span class="string">"key"</span> <span class="attr">value</span>=<span class="string">"要赋的值"</span>/></span></span><br><span class="line"><span class="tag">< /<span class="attr">bean</span>></span> </span><br></pre></td></tr></tbody></table></figure></div>


</blockquote>
<h3 id="2-3-2-setter-注入"><a href="#2-3-2-setter-注入" class="headerlink" title="2.3.2 - setter 注入"></a>2.3.2 - setter 注入</h3><blockquote>
<p><strong>设有下类，且每个属性都要有对应的 setter 方法</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 包路径;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">A</span> </span>{</span><br><span class="line">	String key;</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setKey</span><span class="params">(String key)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.key = key;</span><br><span class="line">}</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<p><strong>则在配置文件中应如下所示</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"自定义id"</span> <span class="attr">class</span>=<span class="string">"包路径.A"</span>></span></span><br><span class="line"><span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"key"</span> <span class="attr">value</span>=<span class="string">"要赋的值"</span>/></span></span><br><span class="line"><span class="tag">< /<span class="attr">bean</span>></span> </span><br></pre></td></tr></tbody></table></figure></div>
</blockquote>
<h2 id="2-4-对象注入"><a href="#2-4-对象注入" class="headerlink" title="2.4 - 对象注入"></a>2.4 - 对象注入</h2><h3 id="2-4-1-constructor-注入"><a href="#2-4-1-constructor-注入" class="headerlink" title="2.4.1 - constructor 注入"></a>2.4.1 - constructor 注入</h3><blockquote>
<p><strong>设有以下两个类，<em>B</em> 中有 <em>A</em> 类属性，则 <em>B</em> 类中要有对应的 <em>setter</em> 方法</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 包路径;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">A</span> </span>{</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 包路径;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">B</span> </span>{</span><br><span class="line"></span><br><span class="line">A a;</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">B</span><span class="params">(A a)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.a = a;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<p><strong>则在配置文件中应如下所示</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"a"</span> <span class="attr">class</span>=<span class="string">"包路径.A"</span>/></span></span><br><span class="line"><span class="tag"><<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"b"</span> <span class="attr">class</span>=<span class="string">"包路径.B"</span>></span></span><br><span class="line">	<span class="tag"><<span class="name">constructor-arg</span> <span class="attr">name</span>=<span class="string">"a"</span> <span class="attr">ref</span>=<span class="string">"a"</span>/></span></span><br><span class="line"><span class="tag">< /<span class="attr">bean</span>></span></span><br></pre></td></tr></tbody></table></figure></div>
</blockquote>
<h3 id="2-4-2-setter-注入"><a href="#2-4-2-setter-注入" class="headerlink" title="2.4.2 - setter 注入"></a>2.4.2 - setter 注入</h3><blockquote>
<p><strong>设有以下两个类，<em>B</em> 中有 <em>A</em> 类属性，则 <em>B</em> 类中要有对应的 <em>setter</em> 方法</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 包路径;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">A</span> </span>{</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 包路径;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">B</span> </span>{</span><br><span class="line"></span><br><span class="line">A a;</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setA</span><span class="params">(A a)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.a = a;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<p><strong>则在配置文件中应如下所示</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"a"</span> <span class="attr">class</span>=<span class="string">"包路径.A"</span>/></span></span><br><span class="line"><span class="tag"><<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"b"</span> <span class="attr">class</span>=<span class="string">"包路径.B"</span>></span></span><br><span class="line">	<span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"a"</span> <span class="attr">ref</span>=<span class="string">"a"</span>/></span></span><br><span class="line"><span class="tag">< /<span class="attr">bean</span>></span></span><br></pre></td></tr></tbody></table></figure></div>
</blockquote>
<h2 id="2-6-IOC-和-DI-的区别"><a href="#2-6-IOC-和-DI-的区别" class="headerlink" title="2.6 - IOC 和 DI 的区别"></a>2.6 - IOC 和 DI 的区别</h2><blockquote>
<ul>
<li><code>IOC</code>：控制反转，把对象创建交给 <code>spring</code> 进行配置</li>
<li><code>DI</code>：依赖注入，向类里面的属性中设置值</li>
<li>依赖注入不能单独存在，需要在 <code>IOC</code> 基础之上完成操作</li>
</ul>
</blockquote>
<h2 id="2-7-注解创建对象"><a href="#2-7-注解创建对象" class="headerlink" title="2.7 - 注解创建对象"></a>2.7 - 注解创建对象</h2><h3 id="2-7-1-注解"><a href="#2-7-1-注解" class="headerlink" title="2.7.1 - 注解"></a>2.7.1 - 注解</h3><blockquote>
<p><strong>第一步：引入约束</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">http://www.springframework.org/schema/context</span><br><span class="line">http://www.springframework.org/schema/context/spring-context.xsd</span><br></pre></td></tr></tbody></table></figure></div>
</blockquote>
<blockquote>
<p><strong>第二步：添加到配置文件</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">context:component-scan</span> <span class="attr">base-package</span>=<span class="string">"包路径"</span>/></span></span><br><span class="line"><span class="tag"><<span class="name">context:annotation-config</span>/></span></span><br></pre></td></tr></tbody></table></figure></div>
</blockquote>
<h4 id="Component"><a href="#Component" class="headerlink" title="- @Component()"></a>- @Component()</h4><blockquote>
<p>用于创建对象，取的名字在括号中赋值给 <code>value</code></p>
</blockquote>
<h4 id="Controller"><a href="#Controller" class="headerlink" title="- @Controller()"></a>- @Controller()</h4><blockquote>
<p><code>Component</code> 的衍生，用于 <code>web</code> 层</p>
</blockquote>
<h4 id="Service"><a href="#Service" class="headerlink" title="- @Service()"></a>- @Service()</h4><blockquote>
<p><code>Component</code> 的衍生，用于业务层</p>
</blockquote>
<h4 id="Repository"><a href="#Repository" class="headerlink" title="- @Repository()"></a>- @Repository()</h4><blockquote>
<p><code>Component</code> 的衍生，用于持久层</p>
</blockquote>
<h4 id="Scope"><a href="#Scope" class="headerlink" title="- @Scope()"></a>- @Scope()</h4><blockquote>
<p>功能和配置文件里的 <code>Scope</code> 一样，值在括号中赋值给 <code>value</code></p>
</blockquote>
<h4 id="Autowired"><a href="#Autowired" class="headerlink" title="- @Autowired"></a>- @Autowired</h4><blockquote>
<p>自动注入其下的属性</p>
</blockquote>
<h4 id="Resource"><a href="#Resource" class="headerlink" title="- @Resource()"></a>- @Resource()</h4><blockquote>
<p>自动注入属性，在括号中通过 <code>name</code> 指定对应的 <code>bean</code> ，更准确，更常用</p>
</blockquote>
<h4 id="Aspect"><a href="#Aspect" class="headerlink" title="- @Aspect"></a>- @Aspect</h4><blockquote>
<p>用于增强所在类</p>
</blockquote>
<h4 id="Before"><a href="#Before" class="headerlink" title="- @Before()"></a>- @Before()</h4><blockquote>
<p>前置增强</p>
</blockquote>
<h4 id="AfterReturning"><a href="#AfterReturning" class="headerlink" title="- @AfterReturning()"></a>- @AfterReturning()</h4><blockquote>
<p>后置增强</p>
</blockquote>
<h4 id="Around"><a href="#Around" class="headerlink" title="- @Around()"></a>- @Around()</h4><blockquote>
<p>环绕增强</p>
</blockquote>
<h4 id="AfterThrowing"><a href="#AfterThrowing" class="headerlink" title="- @AfterThrowing()"></a>- @AfterThrowing()</h4><blockquote>
<p>异常增强</p>
</blockquote>
<h4 id="After"><a href="#After" class="headerlink" title="- @After()"></a>- @After()</h4><blockquote>
<p>最终增强</p>
</blockquote>
<h4 id="Transactional"><a href="#Transactional" class="headerlink" title="- @Transactional"></a>- @Transactional</h4><blockquote>
<p>事务增强</p>
</blockquote>
<h3 id="2-7-2-使用注解创建对象"><a href="#2-7-2-使用注解创建对象" class="headerlink" title="2.7.2 - 使用注解创建对象"></a>2.7.2 - 使用注解创建对象</h3><blockquote>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Component</span>(<span class="string">"a"</span>)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">A</span> </span>{</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<p>就可以用 <code>getBean("a")</code> 创建对象了</p>
</blockquote>
<h3 id="2-7-3-使用注解注入属性"><a href="#2-7-3-使用注解注入属性" class="headerlink" title="2.7.3 - 使用注解注入属性"></a>2.7.3 - 使用注解注入属性</h3><blockquote>
<p><strong>设有 <em>A</em> 类</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 包路径;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">A</span></span>{</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></tbody></table></figure></div>

<p><strong>写入配置</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"自定义id"</span> <span class="attr">class</span>=<span class="string">"包路径"</span>/></span></span><br><span class="line"></span><br></pre></td></tr></tbody></table></figure></div>
</blockquote>
<h4 id="使用-Autowired-注入"><a href="#使用-Autowired-注入" class="headerlink" title="- 使用 @Autowired 注入"></a>- 使用 @Autowired 注入</h4><blockquote>
<p><strong>设有 <em>B</em> 类</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">B</span> </span>{</span><br><span class="line"><span class="meta">@Autowired</span></span><br><span class="line">A a;</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></tbody></table></figure></div>

<p>在属性上方加上 <code>@Autowired</code> 注解即可</p>
</blockquote>
<h4 id="使用-Resource-注入"><a href="#使用-Resource-注入" class="headerlink" title="- 使用 @Resource 注入"></a>- 使用 @Resource 注入</h4><blockquote>
<p><strong>设有 <em>B</em> 类</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">B</span> </span>{</span><br><span class="line"><span class="meta">@Resource</span>(name=<span class="string">"自定义id"</span>)</span><br><span class="line">A a;</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></tbody></table></figure></div>
</blockquote>
<h1 id="三、AOP-思想"><a href="#三、AOP-思想" class="headerlink" title="三、AOP 思想"></a>三、AOP 思想</h1><h2 id="3-1-相关术语"><a href="#3-1-相关术语" class="headerlink" title="3.1 - 相关术语"></a>3.1 - 相关术语</h2><blockquote>
<p><strong>JoinPoint - 连接点</strong></p>
<p>类里面哪些方法可以被增强</p>
</blockquote>
<blockquote>
<p><strong>PointCut - 切入点</strong></p>
<p>被增强了的方法</p>
</blockquote>
<blockquote>
<p><strong>Advice - 通知/增强</strong></p>
<p>增强的逻辑，增加一个什么功能</p>
<ul>
<li>前置增强 – 在切入点之前执行</li>
<li>后置增强 – 在切入点之后执行</li>
<li>异常增强 – 在切入点出现异常时执行</li>
<li>最终增强 – 在后置增强之前执行</li>
<li>环绕增强 – 在切入点之前和之后执行</li>
</ul>
</blockquote>
<blockquote>
<p><strong>Aspect - 切面</strong></p>
<p>把增强应用到切入点的过程</p>
</blockquote>
<blockquote>
<p><strong>Introduction - 引介</strong></p>
<p>在不修改类的代码的前提下，可以在运行期动态的添加一些方法</p>
</blockquote>
<blockquote>
<p><strong>Target - 目标对象</strong></p>
<p>要增强的类</p>
</blockquote>
<blockquote>
<p><strong>Weaving - 织入</strong></p>
<p>把增强应用到目标的过程</p>
</blockquote>
<blockquote>
<p><strong>Proxy - 代理</strong></p>
<p>一个类被 <code>AOP</code> 织入增强后，就产生一个结果代理类</p>
</blockquote>
<h2 id="3-2-AspectJ"><a href="#3-2-AspectJ" class="headerlink" title="3.2 - AspectJ"></a>3.2 - AspectJ</h2><blockquote>
<p>用来实现 <code>AOP</code> 操作</p>
<ul>
<li><code>AspectJ</code> 不是 <code>Spring</code> 的一部分</li>
</ul>
</blockquote>
<h2 id="3-4-环境准备"><a href="#3-4-环境准备" class="headerlink" title="3.4 - 环境准备"></a>3.4 - 环境准备</h2><blockquote>
<p><strong>引入约束</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">http://www.springframework.org/schema/aop</span><br><span class="line">http://www.springframework.org/schema/aop/spring-aop.xsd</span><br><span class="line"></span><br></pre></td></tr></tbody></table></figure></div>
</blockquote>
<h2 id="3-3-配置文件实现-AOP"><a href="#3-3-配置文件实现-AOP" class="headerlink" title="3.3 - 配置文件实现 AOP"></a>3.3 - 配置文件实现 AOP</h2><blockquote>
<p>有如下两个类</p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 包路径;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">A</span> </span>{</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">front</span><span class="params">()</span> </span>{</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></tbody></table></figure></div>

<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 包路径;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">B</span> </span>{</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">met</span><span class="params">()</span> </span>{</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></tbody></table></figure></div>

<p>在 <code>xml</code> 中配置如下</p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">aop:config</span>></span></span><br><span class="line">// 配置切入点</span><br><span class="line"><span class="tag"><<span class="name">aop:pointcut</span> <span class="attr">id</span>=<span class="string">"切入点名称"</span> <span class="attr">expression</span>=<span class="string">"execution(* *.*())"</span>/></span></span><br><span class="line">// 配置切入面</span><br><span class="line"><span class="tag"><<span class="name">aop:aspect</span> <span class="attr">ref</span>=<span class="string">"增强所在类的 bean-id"</span>></span></span><br><span class="line"> // 配置增强的方法，其中 method 指用哪个类来增强，pointcut-ref 指切入点</span><br><span class="line"> <span class="tag"><<span class="name">aop:before</span> <span class="attr">method</span>=<span class="string">"front"</span> <span class="attr">pointcut-ref</span>=<span class="string">"切入点名称"</span>/></span></span><br><span class="line"><span class="tag">< /<span class="attr">aop:aspect</span>></span></span><br><span class="line"><span class="tag">< /<span class="attr">aop:config</span>></span></span><br><span class="line"></span><br><span class="line">- execution(* *.*(..)) 表达式</span><br><span class="line">- 第一个 * 号是返回类型</span><br><span class="line">- 第二个 * 号是切入点所在类的全路径</span><br><span class="line">- 第三个 * 号是切入点</span><br><span class="line"></span><br></pre></td></tr></tbody></table></figure></div>
</blockquote>
<h2 id="3-5-注解方式实现-AOP"><a href="#3-5-注解方式实现-AOP" class="headerlink" title="3.5 - 注解方式实现 AOP"></a>3.5 - 注解方式实现 AOP</h2><blockquote>
<p>添加至 <code>xml</code> 配置文件</p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">aop:aspectj-autoproxy</span>/></span></span><br><span class="line"></span><br></pre></td></tr></tbody></table></figure></div>

<p>有下面两个类</p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 包路径;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Aspect</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">A</span> </span>{</span><br><span class="line"><span class="meta">@Before</span>(value=<span class="string">"execution(* B的全路径.要增强的方法())"</span>)</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">front</span><span class="params">()</span> </span>{</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></tbody></table></figure></div>

<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 包路径;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">B</span> </span>{</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">met</span><span class="params">()</span> </span>{</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></tbody></table></figure></div>
</blockquote>
<h1 id="四、JDBC-Template"><a href="#四、JDBC-Template" class="headerlink" title="四、JDBC Template"></a>四、JDBC Template</h1><h2 id="4-1-环境配置"><a href="#4-1-环境配置" class="headerlink" title="4.1 - 环境配置"></a>4.1 - 环境配置</h2><h3 id="4-1-1-导入-jar-包"><a href="#4-1-1-导入-jar-包" class="headerlink" title="4.1.1 - 导入 jar 包"></a>4.1.1 - 导入 jar 包</h3><blockquote>
<ul>
<li><code>c3p0</code></li>
<li><code>mysql-connector-java</code></li>
<li><code>mchange-commons-java</code></li>
</ul>
</blockquote>
<h3 id="4-1-2-配置连接池"><a href="#4-1-2-配置连接池" class="headerlink" title="4.1.2 - 配置连接池"></a>4.1.2 - 配置连接池</h3><p>在 <code>xml</code> 中配置</p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"dataSource"</span> <span class="attr">class</span>=<span class="string">"com.mchange.v2.c3p0.ComboPooledDataSource"</span>></span></span><br><span class="line">  <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"driverClass"</span> <span class="attr">value</span>=<span class="string">"com.mysql.jdbc.Driver"</span>/></span></span><br><span class="line">  <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"jdbcUrl"</span> <span class="attr">value</span>=<span class="string">"jdbc:mysql:///表名"</span>/></span></span><br><span class="line">  <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"user"</span> <span class="attr">value</span>=<span class="string">"账号"</span>/></span></span><br><span class="line">  <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"password"</span> <span class="attr">value</span>=<span class="string">"密码"</span>/></span></span><br><span class="line"><span class="tag">< /<span class="attr">bean</span>></span></span><br></pre></td></tr></tbody></table></figure></div>

<h3 id="4-1-3-配置-JDBC-Template"><a href="#4-1-3-配置-JDBC-Template" class="headerlink" title="4.1.3 - 配置 JDBC Template"></a>4.1.3 - 配置 JDBC Template</h3><p>在 <code>xml</code> 中配置，并注入 <code>dataSource</code></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"jdbcTemplate"</span> <span class="attr">class</span>=<span class="string">"org.springframework.jdbc.core.JdbcTemplate"</span>></span></span><br><span class="line">  <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"dataSource"</span> <span class="attr">ref</span>=<span class="string">"dataSource"</span>/></span></span><br><span class="line"><span class="tag">< /<span class="attr">bean</span>></span></span><br></pre></td></tr></tbody></table></figure></div>

<h3 id="4-1-4-配置-Dao"><a href="#4-1-4-配置-Dao" class="headerlink" title="4.1.4 - 配置 Dao"></a>4.1.4 - 配置 Dao</h3><blockquote>
<p>新建类，定义 <code>JdbcTemplate</code>，设置 setter 方法</p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 包路径;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">A</span> </span>{</span><br><span class="line"><span class="keyword">private</span> JdbcTemplate jdbc;</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setJdbc</span><span class="params">(JdbcTemplate jdbc)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.jdbc = jdbc;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></tbody></table></figure></div>

<p>利用 <code>xml</code> 配置文件注入</p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"自定义"</span> <span class="attr">class</span>=<span class="string">"包路径.A"</span>></span></span><br><span class="line"><span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"jdbc"</span> <span class="attr">ref</span>=<span class="string">"jdbcTemplate"</span>/></span></span><br><span class="line"><span class="tag">< /<span class="attr">bean</span>></span></span><br><span class="line"></span><br></pre></td></tr></tbody></table></figure></div>
</blockquote>
<h2 id="4-2-增"><a href="#4-2-增" class="headerlink" title="4.2 - 增"></a>4.2 - 增</h2><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">String sql = <span class="string">"insert into 表名 values(···)"</span>;</span><br><span class="line">jdbc.update(sql,···);</span><br></pre></td></tr></tbody></table></figure></div>

<h2 id="4-3-删"><a href="#4-3-删" class="headerlink" title="4.3 - 删"></a>4.3 - 删</h2><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">String sql = <span class="string">"delete from 表名 where 条件"</span>;</span><br><span class="line">jdbc.update(sql,···);</span><br></pre></td></tr></tbody></table></figure></div>

<h2 id="4-4-查"><a href="#4-4-查" class="headerlink" title="4.4 - 查"></a>4.4 - 查</h2><h3 id="4-4-1-查询某一个返回值"><a href="#4-4-1-查询某一个返回值" class="headerlink" title="4.4.1 - 查询某一个返回值"></a>4.4.1 - 查询某一个返回值</h3><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">String sql = <span class="string">"select count(*) from 表名"</span>;</span><br><span class="line">jdbc.queryFroObject(sql, 希望返回的类型);</span><br></pre></td></tr></tbody></table></figure></div>

<h3 id="4-4-2-查询后将一行结果封装到一个对象"><a href="#4-4-2-查询后将一行结果封装到一个对象" class="headerlink" title="4.4.2 - 查询后将一行结果封装到一个对象"></a>4.4.2 - 查询后将一行结果封装到一个对象</h3><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">String sql = <span class="string">"select * from 表名 where 条件"</span>;</span><br><span class="line">结果类 变量 = jdbc.queryForObject(sql, <span class="keyword">new</span> 映射类, ···);</span><br></pre></td></tr></tbody></table></figure></div>

<p><strong>结果类需自己写</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> 映射类 <span class="keyword">implements</span> <span class="title">RowMapper</span><结果类> </span>{</span><br><span class="line">  </span><br><span class="line">  <span class="keyword">public</span> 结果类 mapRow(ResultSet rs, <span class="keyword">int</span> num) <span class="keyword">throws</span> SQLException {</span><br><span class="line">    </span><br><span class="line">    结果类 变量 = <span class="keyword">new</span> 结果类();</span><br><span class="line">    变量.属性<span class="number">1</span> = rs.getString(<span class="string">"对应字段1"</span>);</span><br><span class="line">    ···</span><br><span class="line">    变量.属性n = rs.getString(<span class="string">"对应字段n"</span>);</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> 变量;</span><br><span class="line">    </span><br><span class="line">  }</span><br><span class="line">  </span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<h3 id="4-4-2-查询后将一行结果封装到一个列表"><a href="#4-4-2-查询后将一行结果封装到一个列表" class="headerlink" title="4.4.2 - 查询后将一行结果封装到一个列表"></a>4.4.2 - 查询后将一行结果封装到一个列表</h3><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">String sql = <span class="string">"select * from 表名 where 条件"</span>;</span><br><span class="line">List<结果类> 变量 = jdbc.query(sql, <span class="keyword">new</span> 映射类, ···);</span><br></pre></td></tr></tbody></table></figure></div>

<p><strong>结果类需自己写</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> 映射类 <span class="keyword">implements</span> <span class="title">RowMapper</span><结果类> </span>{</span><br><span class="line">  </span><br><span class="line">  <span class="keyword">public</span> 结果类 mapRow(ResultSet rs, <span class="keyword">int</span> num) <span class="keyword">throws</span> SQLException {</span><br><span class="line">    </span><br><span class="line">    结果类 变量 = <span class="keyword">new</span> 结果类();</span><br><span class="line">    变量.属性<span class="number">1</span> = rs.getString(<span class="string">"对应字段1"</span>);</span><br><span class="line">    ···</span><br><span class="line">    变量.属性n = rs.getString(<span class="string">"对应字段n"</span>);</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> 变量;</span><br><span class="line">    </span><br><span class="line">  }</span><br><span class="line">  </span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>



<h2 id="4-5-改"><a href="#4-5-改" class="headerlink" title="4.5 - 改"></a>4.5 - 改</h2><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">String sql = <span class="string">"update 表名 set 字段名 = ? where 条件"</span>;</span><br><span class="line">jdbc.update(sql,···);</span><br></pre></td></tr></tbody></table></figure></div>

<h2 id="4-6-事务管理"><a href="#4-6-事务管理" class="headerlink" title="4.6 - 事务管理"></a>4.6 - 事务管理</h2><h3 id="4-6-1-环境配置"><a href="#4-6-1-环境配置" class="headerlink" title="4.6.1 - 环境配置"></a>4.6.1 - 环境配置</h3><h4 id="引入约束"><a href="#引入约束" class="headerlink" title="- 引入约束"></a>- 引入约束</h4><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">http://www.springframework.org/schema/tx</span><br><span class="line">http://www.springframework.org/schema/tx/spring-tx.xsd</span><br></pre></td></tr></tbody></table></figure></div>

<h3 id="4-6-2-配置文件方式"><a href="#4-6-2-配置文件方式" class="headerlink" title="4.6.2 - 配置文件方式"></a>4.6.2 - 配置文件方式</h3><h4 id="创建事务管理器"><a href="#创建事务管理器" class="headerlink" title="- 创建事务管理器"></a>- 创建事务管理器</h4><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"管理器名称"</span> <span class="attr">class</span>=<span class="string">"org.springframework.jdbc.datasource.DataSourceTransactionManager"</span>></span></span><br><span class="line">  <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"dataSource"</span> <span class="attr">ref</span>=<span class="string">"dataSource"</span>/></span></span><br><span class="line"><span class="tag">< /<span class="attr">bean</span>></span></span><br></pre></td></tr></tbody></table></figure></div>

<h4 id="配置事务增强"><a href="#配置事务增强" class="headerlink" title="- 配置事务增强"></a>- 配置事务增强</h4><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">tx:advice</span> <span class="attr">id</span>=<span class="string">"增强名称"</span> <span class="attr">transaction-manager</span>=<span class="string">"管理器名称"</span>></span></span><br><span class="line">  <span class="tag"><<span class="name">tx:attributes</span>></span></span><br><span class="line">    <span class="tag"><<span class="name">tx:method</span> <span class="attr">name</span>=<span class="string">"匹配的方法名"</span>/></span></span><br><span class="line">  <span class="tag">< /<span class="attr">tx:attributes</span>></span></span><br><span class="line"><span class="tag">< /<span class="attr">tx:advice</span>></span></span><br><span class="line"></span><br><span class="line">- 此处的方法是指要进行事务管理的方法</span><br></pre></td></tr></tbody></table></figure></div>

<h4 id="配置切面"><a href="#配置切面" class="headerlink" title="- 配置切面"></a>- 配置切面</h4><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">aop:config</span>></span></span><br><span class="line">  <span class="tag"><<span class="name">aop:pointcut</span> <span class="attr">id</span>=<span class="string">"切入点名称"</span> <span class="attr">expression</span>=<span class="string">"execution(返回类型 该方法所在全路径.方法名())"</span>/></span></span><br><span class="line">  <span class="tag"><<span class="name">aop:advisor</span> <span class="attr">advice-ref</span>=<span class="string">"增强名称"</span> <span class="attr">pointcut-ref</span>=<span class="string">"切入点名称"</span>/></span></span><br><span class="line"><span class="tag">< /<span class="attr">aop:config</span>></span></span><br><span class="line"></span><br><span class="line">- 此处的方法是指要进行事务管理的方法</span><br></pre></td></tr></tbody></table></figure></div>

<h3 id="4-6-3-注解方式"><a href="#4-6-3-注解方式" class="headerlink" title="4.6.3 - 注解方式"></a>4.6.3 - 注解方式</h3><h4 id="配置事务管理器"><a href="#配置事务管理器" class="headerlink" title="- 配置事务管理器"></a>- 配置事务管理器</h4><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"管理器名称"</span> <span class="attr">class</span>=<span class="string">"org.springframework.jdbc.datasource.DataSourceTransactionManager"</span>></span></span><br><span class="line">  <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"dataSource"</span> <span class="attr">ref</span>=<span class="string">"dataSource"</span>/></span></span><br><span class="line"><span class="tag">< /<span class="attr">bean</span>></span></span><br></pre></td></tr></tbody></table></figure></div>

<h4 id="开启事务注解"><a href="#开启事务注解" class="headerlink" title="- 开启事务注解"></a>- 开启事务注解</h4><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">tx:annotation-driven</span> <span class="attr">transaction-manager</span>=<span class="string">"transactionManager"</span>/></span></span><br></pre></td></tr></tbody></table></figure></div>

<h4 id="使用"><a href="#使用" class="headerlink" title="- 使用"></a>- 使用</h4><p>在需要事务管理的类上面加上注解 <code>@Transactional</code></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Transactional</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">A</span> </span>{</span><br><span class="line">  </span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

</body></html></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">Author: </span><span class="post-copyright-info"><a href="mailto:undefined">Cying-承影</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">Link: </span><span class="post-copyright-info"><a href="http://cyingk.github.io/2020/01/27/Spring/">http://cyingk.github.io/2020/01/27/Spring/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">Copyright Notice: </span><span class="post-copyright-info">All articles in this blog are licensed under <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> unless stating additionally.</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/JavaEE/">JavaEE</a></div><div class="post_share"></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2020/01/31/Http_Servlet/"><img class="prev-cover" src="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3567041170,3790666556&amp;fm=26&amp;gp=0.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">Previous Post</div><div class="prev_info">Http Servlet</div></div></a></div><div class="next-post pull-right"><a href="/2020/01/16/Design_Pattern/"><img class="next-cover" src="https://www.runoob.com/wp-content/uploads/2014/08/the-relationship-between-design-patterns.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">Next Post</div><div class="next_info">Design Pattern</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> Related Articles</span></div><div class="relatedPosts-list"><div><a href="/2020/08/06/Linux-And-Docker/" title="SpringCloud With Docker"><img class="cover" src="https://img-blog.csdnimg.cn/202008061653587.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-08-06</div><div class="title">SpringCloud With Docker</div></div></a></div><div><a href="/2020/05/05/SpringCloud/" title="SpringCloud"><img class="cover" src="http://img3.imgtn.bdimg.com/it/u=1799420697,1254911040&fm=26&gp=0.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-05-05</div><div class="title">SpringCloud</div></div></a></div><div><a href="/2020/04/05/SpringBoot/" title="SpringBoot"><img class="cover" src="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=4157804759,1050439081&fm=26&gp=0.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-04-05</div><div class="title">SpringBoot</div></div></a></div><div><a href="/2020/02/20/Mybatis/" title="MyBatis"><img class="cover" src="https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=575314515,4268715674&fm=26&gp=0.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-02-20</div><div class="title">MyBatis</div></div></a></div><div><a href="/2020/02/16/SpringMVC/" title="SpringMVC"><img class="cover" src="https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=790255512,3591398303&fm=26&gp=0.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-02-16</div><div class="title">SpringMVC</div></div></a></div><div><a href="/2020/02/15/Hibernate/" title="Hibernate"><img class="cover" src="https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=607365457,3589909035&fm=26&gp=0.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-02-15</div><div class="title">Hibernate</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>Catalog</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link"><span class="toc-number">1.</span> <span class="toc-text">Spring</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%80%E3%80%81Spring-%E6%A6%82%E8%BF%B0"><span class="toc-number">2.</span> <span class="toc-text">一、Spring 概述</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-1-%E4%BB%80%E4%B9%88%E6%98%AF-Spring"><span class="toc-number">2.1.</span> <span class="toc-text">1.1 - 什么是 Spring</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-3-Spring-%E7%9A%84%E4%BC%98%E5%8A%BF"><span class="toc-number">2.2.</span> <span class="toc-text">1.3 - Spring 的优势</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-4-Spring-%E4%BD%93%E7%B3%BB%E7%BB%93%E6%9E%84"><span class="toc-number">2.3.</span> <span class="toc-text">1.4 - Spring 体系结构</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-5-%E8%80%A6%E5%90%88"><span class="toc-number">2.4.</span> <span class="toc-text">1.5 - 耦合</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-6-Bean%E5%AF%B9%E8%B1%A1"><span class="toc-number">2.5.</span> <span class="toc-text">1.6 - Bean对象</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-7-%E8%A7%A3%E5%86%B3%E6%8A%80%E6%9C%AF"><span class="toc-number">2.6.</span> <span class="toc-text">1.7 - 解决技术</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BA%8C%E3%80%81IOC-%E6%8E%A7%E5%88%B6%E5%8F%8D%E8%BD%AC"><span class="toc-number">3.</span> <span class="toc-text">二、IOC 控制反转</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#2-1-IOC-%E7%9A%84%E5%BA%95%E5%B1%82%E5%8E%9F%E7%90%86"><span class="toc-number">3.1.</span> <span class="toc-text">2.1 - IOC 的底层原理</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-1-1-IOC-%E7%9A%84%E5%AE%9E%E7%8E%B0"><span class="toc-number">3.1.1.</span> <span class="toc-text">2.1.1 - IOC 的实现</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-1-2-IOC-%E5%85%A5%E9%97%A8%E6%A1%88%E4%BE%8B"><span class="toc-number">3.1.2.</span> <span class="toc-text">2.1.2 - IOC 入门案例</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-2-bean-%E7%AE%A1%E7%90%86"><span class="toc-number">3.2.</span> <span class="toc-text">2.2 - bean 管理</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-2-1-%E5%AE%9E%E4%BE%8B%E5%8C%96%E7%9A%84%E4%B8%89%E7%A7%8D%E6%96%B9%E5%BC%8F"><span class="toc-number">3.2.1.</span> <span class="toc-text">2.2.1 - 实例化的三种方式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-2-2-Bean-%E7%9A%84%E5%B8%B8%E7%94%A8%E5%B1%9E%E6%80%A7"><span class="toc-number">3.2.2.</span> <span class="toc-text">2.2.2 - Bean 的常用属性</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-3-%E5%B1%9E%E6%80%A7%E6%B3%A8%E5%85%A5"><span class="toc-number">3.3.</span> <span class="toc-text">2.3 - 属性注入</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-3-1-constructor-%E6%B3%A8%E5%85%A5"><span class="toc-number">3.3.1.</span> <span class="toc-text">2.3.1 - constructor 注入</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-3-2-setter-%E6%B3%A8%E5%85%A5"><span class="toc-number">3.3.2.</span> <span class="toc-text">2.3.2 - setter 注入</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-4-%E5%AF%B9%E8%B1%A1%E6%B3%A8%E5%85%A5"><span class="toc-number">3.4.</span> <span class="toc-text">2.4 - 对象注入</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-4-1-constructor-%E6%B3%A8%E5%85%A5"><span class="toc-number">3.4.1.</span> <span class="toc-text">2.4.1 - constructor 注入</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-4-2-setter-%E6%B3%A8%E5%85%A5"><span class="toc-number">3.4.2.</span> <span class="toc-text">2.4.2 - setter 注入</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-6-IOC-%E5%92%8C-DI-%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="toc-number">3.5.</span> <span class="toc-text">2.6 - IOC 和 DI 的区别</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-7-%E6%B3%A8%E8%A7%A3%E5%88%9B%E5%BB%BA%E5%AF%B9%E8%B1%A1"><span class="toc-number">3.6.</span> <span class="toc-text">2.7 - 注解创建对象</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-7-1-%E6%B3%A8%E8%A7%A3"><span class="toc-number">3.6.1.</span> <span class="toc-text">2.7.1 - 注解</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#Component"><span class="toc-number">3.6.1.1.</span> <span class="toc-text">- @Component()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Controller"><span class="toc-number">3.6.1.2.</span> <span class="toc-text">- @Controller()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Service"><span class="toc-number">3.6.1.3.</span> <span class="toc-text">- @Service()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Repository"><span class="toc-number">3.6.1.4.</span> <span class="toc-text">- @Repository()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Scope"><span class="toc-number">3.6.1.5.</span> <span class="toc-text">- @Scope()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Autowired"><span class="toc-number">3.6.1.6.</span> <span class="toc-text">- @Autowired</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Resource"><span class="toc-number">3.6.1.7.</span> <span class="toc-text">- @Resource()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Aspect"><span class="toc-number">3.6.1.8.</span> <span class="toc-text">- @Aspect</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Before"><span class="toc-number">3.6.1.9.</span> <span class="toc-text">- @Before()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#AfterReturning"><span class="toc-number">3.6.1.10.</span> <span class="toc-text">- @AfterReturning()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Around"><span class="toc-number">3.6.1.11.</span> <span class="toc-text">- @Around()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#AfterThrowing"><span class="toc-number">3.6.1.12.</span> <span class="toc-text">- @AfterThrowing()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#After"><span class="toc-number">3.6.1.13.</span> <span class="toc-text">- @After()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Transactional"><span class="toc-number">3.6.1.14.</span> <span class="toc-text">- @Transactional</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-7-2-%E4%BD%BF%E7%94%A8%E6%B3%A8%E8%A7%A3%E5%88%9B%E5%BB%BA%E5%AF%B9%E8%B1%A1"><span class="toc-number">3.6.2.</span> <span class="toc-text">2.7.2 - 使用注解创建对象</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-7-3-%E4%BD%BF%E7%94%A8%E6%B3%A8%E8%A7%A3%E6%B3%A8%E5%85%A5%E5%B1%9E%E6%80%A7"><span class="toc-number">3.6.3.</span> <span class="toc-text">2.7.3 - 使用注解注入属性</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8-Autowired-%E6%B3%A8%E5%85%A5"><span class="toc-number">3.6.3.1.</span> <span class="toc-text">- 使用 @Autowired 注入</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8-Resource-%E6%B3%A8%E5%85%A5"><span class="toc-number">3.6.3.2.</span> <span class="toc-text">- 使用 @Resource 注入</span></a></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%89%E3%80%81AOP-%E6%80%9D%E6%83%B3"><span class="toc-number">4.</span> <span class="toc-text">三、AOP 思想</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#3-1-%E7%9B%B8%E5%85%B3%E6%9C%AF%E8%AF%AD"><span class="toc-number">4.1.</span> <span class="toc-text">3.1 - 相关术语</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-2-AspectJ"><span class="toc-number">4.2.</span> <span class="toc-text">3.2 - AspectJ</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-4-%E7%8E%AF%E5%A2%83%E5%87%86%E5%A4%87"><span class="toc-number">4.3.</span> <span class="toc-text">3.4 - 环境准备</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-3-%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E5%AE%9E%E7%8E%B0-AOP"><span class="toc-number">4.4.</span> <span class="toc-text">3.3 - 配置文件实现 AOP</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-5-%E6%B3%A8%E8%A7%A3%E6%96%B9%E5%BC%8F%E5%AE%9E%E7%8E%B0-AOP"><span class="toc-number">4.5.</span> <span class="toc-text">3.5 - 注解方式实现 AOP</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%9B%9B%E3%80%81JDBC-Template"><span class="toc-number">5.</span> <span class="toc-text">四、JDBC Template</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#4-1-%E7%8E%AF%E5%A2%83%E9%85%8D%E7%BD%AE"><span class="toc-number">5.1.</span> <span class="toc-text">4.1 - 环境配置</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#4-1-1-%E5%AF%BC%E5%85%A5-jar-%E5%8C%85"><span class="toc-number">5.1.1.</span> <span class="toc-text">4.1.1 - 导入 jar 包</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-1-2-%E9%85%8D%E7%BD%AE%E8%BF%9E%E6%8E%A5%E6%B1%A0"><span class="toc-number">5.1.2.</span> <span class="toc-text">4.1.2 - 配置连接池</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-1-3-%E9%85%8D%E7%BD%AE-JDBC-Template"><span class="toc-number">5.1.3.</span> <span class="toc-text">4.1.3 - 配置 JDBC Template</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-1-4-%E9%85%8D%E7%BD%AE-Dao"><span class="toc-number">5.1.4.</span> <span class="toc-text">4.1.4 - 配置 Dao</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-2-%E5%A2%9E"><span class="toc-number">5.2.</span> <span class="toc-text">4.2 - 增</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-3-%E5%88%A0"><span class="toc-number">5.3.</span> <span class="toc-text">4.3 - 删</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-4-%E6%9F%A5"><span class="toc-number">5.4.</span> <span class="toc-text">4.4 - 查</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#4-4-1-%E6%9F%A5%E8%AF%A2%E6%9F%90%E4%B8%80%E4%B8%AA%E8%BF%94%E5%9B%9E%E5%80%BC"><span class="toc-number">5.4.1.</span> <span class="toc-text">4.4.1 - 查询某一个返回值</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-4-2-%E6%9F%A5%E8%AF%A2%E5%90%8E%E5%B0%86%E4%B8%80%E8%A1%8C%E7%BB%93%E6%9E%9C%E5%B0%81%E8%A3%85%E5%88%B0%E4%B8%80%E4%B8%AA%E5%AF%B9%E8%B1%A1"><span class="toc-number">5.4.2.</span> <span class="toc-text">4.4.2 - 查询后将一行结果封装到一个对象</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-4-2-%E6%9F%A5%E8%AF%A2%E5%90%8E%E5%B0%86%E4%B8%80%E8%A1%8C%E7%BB%93%E6%9E%9C%E5%B0%81%E8%A3%85%E5%88%B0%E4%B8%80%E4%B8%AA%E5%88%97%E8%A1%A8"><span class="toc-number">5.4.3.</span> <span class="toc-text">4.4.2 - 查询后将一行结果封装到一个列表</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-5-%E6%94%B9"><span class="toc-number">5.5.</span> <span class="toc-text">4.5 - 改</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-6-%E4%BA%8B%E5%8A%A1%E7%AE%A1%E7%90%86"><span class="toc-number">5.6.</span> <span class="toc-text">4.6 - 事务管理</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#4-6-1-%E7%8E%AF%E5%A2%83%E9%85%8D%E7%BD%AE"><span class="toc-number">5.6.1.</span> <span class="toc-text">4.6.1 - 环境配置</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%BC%95%E5%85%A5%E7%BA%A6%E6%9D%9F"><span class="toc-number">5.6.1.1.</span> <span class="toc-text">- 引入约束</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-6-2-%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E6%96%B9%E5%BC%8F"><span class="toc-number">5.6.2.</span> <span class="toc-text">4.6.2 - 配置文件方式</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E4%BA%8B%E5%8A%A1%E7%AE%A1%E7%90%86%E5%99%A8"><span class="toc-number">5.6.2.1.</span> <span class="toc-text">- 创建事务管理器</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE%E4%BA%8B%E5%8A%A1%E5%A2%9E%E5%BC%BA"><span class="toc-number">5.6.2.2.</span> <span class="toc-text">- 配置事务增强</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE%E5%88%87%E9%9D%A2"><span class="toc-number">5.6.2.3.</span> <span class="toc-text">- 配置切面</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-6-3-%E6%B3%A8%E8%A7%A3%E6%96%B9%E5%BC%8F"><span class="toc-number">5.6.3.</span> <span class="toc-text">4.6.3 - 注解方式</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE%E4%BA%8B%E5%8A%A1%E7%AE%A1%E7%90%86%E5%99%A8"><span class="toc-number">5.6.3.1.</span> <span class="toc-text">- 配置事务管理器</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%BC%80%E5%90%AF%E4%BA%8B%E5%8A%A1%E6%B3%A8%E8%A7%A3"><span class="toc-number">5.6.3.2.</span> <span class="toc-text">- 开启事务注解</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8"><span class="toc-number">5.6.3.3.</span> <span class="toc-text">- 使用</span></a></li></ol></li></ol></li></ol></li></ol></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="footer_custom_text">却道天凉好个秋</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="Read Mode"><i class="fas fa-book-open"></i></button><button id="font-plus" type="button" title="Increase font size"><i class="fas fa-plus"></i></button><button id="font-minus" type="button" title="Decrease font size"><i class="fas fa-minus"></i></button><button id="darkmode" type="button" title="Switch Between Light And Dark Mode"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="Toggle between single-column and double-column"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="Setting"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="Table Of Contents"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="Back To Top"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script>function panguFn () {
  if (typeof pangu === 'object') pangu.autoSpacingPage()
  else {
    getScript('https://cdn.jsdelivr.net/npm/pangu/dist/browser/pangu.min.js')
      .then(() => {
        pangu.autoSpacingPage()
      })
  }
}

function panguInit () {
  if (false){
    GLOBAL_CONFIG_SITE.isPost && panguFn()
  } else {
    panguFn()
  }
}

document.addEventListener('DOMContentLoaded', panguInit)</script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"><script>if (document.getElementsByClassName('mermaid').length) {
  if (window.mermaidJsLoad) mermaid.init()
  else {
    getScript('https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js').then(() => {
      window.mermaidJsLoad = true
      mermaid.initialize({
        theme: 'default',
      })
      false && mermaid.init()
    })
  }
}</script></div><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>